<?php

/**
 * @name: HOTN AnimeList
 * @author: HammerOnTheNail
 * @link: http://code.google.com/p/hotnanimelist/
 */

if($_POST['id']<>"")
{
	$project_id = (int)$_POST['id'];
	
	// Alap dolgok bekérése
	include("../../class2.php");
	include(e_PLUGIN."/hotn_animelist/Languages/".e_LANGUAGE.".php");
	include(e_PLUGIN."hotn_animelist/Class/animelist_mysql.php");
	include(e_PLUGIN."hotn_animelist/Class/animelist_options.php");
	include(e_PLUGIN."hotn_animelist/Class/animelist_project.php");
	
	//beállítások ellenőrzése:
	if ($HOTN_ANIMELIST_OPTIONS['download_type']<>"")
	{
		include(e_PLUGIN."hotn_animelist/Class/download_".$HOTN_ANIMELIST_OPTIONS['download_type'].".php");
		$HOTN_ANIMELIST_DOWNLOAD = new hotn_animelist_download();
	}
	
	$gen = new convert;
	
	// Project adatok lekérdezése
	$project = $HOTN_ANIMELIST_DOWNLOAD->hotn_animelist_download_get_one_project($project_id);
	
	// More icon
	if (file_exists(THEME."hotn_animelist/more.png")) define("HOTN_ANIMELIST_DOWNLOAD_MORE",THEME."hotn_animelist/more.png");
	else define("HOTN_ANIMELIST_DOWNLOAD_MORE",e_PLUGIN."hotn_animelist/Images/more.png");
	
	$project_type = $project['project_type'];
	if ($project['project_type'] == "anime")
	{
		$download["subtitle"] = $HOTN_ANIMELIST_DOWNLOAD->hotn_animelist_download_get_download($project['project_id'],'subtitle');
		$download["video"] = $HOTN_ANIMELIST_DOWNLOAD->hotn_animelist_download_get_download($project['project_id'],'anime');
	}
	else
	{
		$download[$project['project_type']] = $HOTN_ANIMELIST_DOWNLOAD->hotn_animelist_download_get_download($project['project_id'],$project['project_type']);
	}
	
	$download["torrent"] = $HOTN_ANIMELIST_DOWNLOAD->hotn_animelist_download_get_download($project['project_id'],'torrent');
	
	$download_title = $HOTN_ANIMELIST_PROJECT->get_types();
	
	foreach ($download as $download_category_name => $download)
	{
		if (count($download) > 0)
		{
			$text_project_content .= "<table class='hotn_animelist_project_download_".$download_category_name."'>
				<tr>
					<td colspan='2' class='hotn_animelist_project_download_category_title'>
						".$download_title[$download_category_name]."
					</td>
				</tr>";
			
			// manga típusok kiírása
			if ($project_type == "manga")
			{
				foreach ($download as $download_category)
				{
					$text_project_content .= "
						<tr>
							<td class='hotn_animelist_project_download_category_image'>
								<img src='".e_PLUGIN."hotn_animelist/Project/".$project['project_folder']."/".$download_category[0]['category_image']."' />
							</td>
							<td class='hotn_animelist_project_download_category_list'>
								<table>
									<tr class='hotn_animelist_project_download_head'>
										<td class='hotn_animelist_project_download_name_head'>
											".HOTN_ANIMELIST_PROJECT_DOWNLOAD_TITLE."
										</td>
										<td class='hotn_animelist_project_download_link_head'>
											".HOTN_ANIMELIST_PROJECT_DOWNLOAD_DOWNLOAD."
										</td>
									</tr>
					";
					$poz = 0;
					foreach ($download_category as $download_one)
					{
						$text_project_content .= showdownload($download_one,$poz++);
					}
					
					$text_project_content .= "
								</table>
							</td>
						</tr>
					";
				}
			}else{
				$text_project_content .= "<tr class='hotn_animelist_project_download_head'>
									<td class='hotn_animelist_project_download_name_head'>
										".HOTN_ANIMELIST_PROJECT_DOWNLOAD_TITLE."
									</td>
									<td class='hotn_animelist_project_download_link_head'>
										".HOTN_ANIMELIST_PROJECT_DOWNLOAD_DOWNLOAD."
									</td>
								</tr>";
				
				$poz = 0;
				foreach ($download as $download)
				{
					$text_project_content .= showdownload($download,$poz++);
				}
			}
			
			
			$text_project_content .= "</table>";
		}
	}
	
	print $text_project_content;
}

// Letöltés kiírása
function showdownload($download,$poz)
{
	global $gen, $HOTN_ANIMELIST_DOWNLOAD;
	
	if ($poz%2 == 0) $poz_css = "hotn_animelist_project_download_list_highlite";
	
	return "
		<tr class='hotn_animelist_project_download_list ".$poz_css."' id='download_".$download['download_id']."'>
			<td class='hotn_animelist_project_download_name'>
				<a href='javascript:hotn_animelist_show(".$download['download_id'].")'>".$download['download_name']."</a> <a href='javascript:hotn_animelist_show(".$download['download_id'].")'><img src='".HOTN_ANIMELIST_DOWNLOAD_MORE."' /></a>
			</td>
			<td class='hotn_animelist_project_download_link'>
				".$HOTN_ANIMELIST_DOWNLOAD->hotn_animelist_download_link($download['download_id'],strpos($download['download_url'], "http"))."
			</td>
		</tr>
		<tr class='hotn_animelist_project_download_list_more ".$poz_css."_more'>
			<td colspan='2' class='hotn_animelist_project_download_more' id='download_more_".$download['download_id']."'>
				<ul>
					<li class='hotn_animelist_project_download_more_filename'><span>".HOTN_ANIMELIST_PROJECT_DOWNLOAD_FILENAME."</span>".basename($download['download_url'])."</li>
					<li class='hotn_animelist_project_download_more_filename'><span>".HOTN_ANIMELIST_PROJECT_DOWNLOAD_FILESIZE."</span>".parsesize($download['download_filesize'])."</li>
					<li class='hotn_animelist_project_download_more_author'><span>".HOTN_ANIMELIST_PROJECT_DOWNLOAD_AUTHOR."</span>".author($download['download_author'],$download['user_id'])."</li>
					<li class='hotn_animelist_project_download_more_date'><span>".HOTN_ANIMELIST_PROJECT_DOWNLOAD_DATE."</span>".$gen->convert_date($download['download_datestamp'],"long")."</li>
					<li class='hotn_animelist_project_download_more_request'><span>".HOTN_ANIMELIST_PROJECT_DOWNLOAD_REQUEST."</span>".$download['download_request']."</li>
				</ul>
			</td>
		</tr>
	";
}

// author kiírása
function author($name,$id)
{
	if (is_numeric($id))
	{
		return "<a href='project_team.php?".$id."'>".$name."</a>";
	}
	else
	{
		return $name;
	}
	
}

// az eredeti download.php-ból
function parsesize($size) {
	$kb = 1024;
	$mb = 1024 * $kb;
	$gb = 1024 * $mb;
	$tb = 1024 * $gb;
	if(!$size)
	{
		return '0';
	}
	if ($size < $kb) {
		return $size." ".CORE_LAN_B;
	}
	else if($size < $mb) {
		return round($size/$kb, 2)." ".CORE_LAN_KB;
	}
	else if($size < $gb) {
		return round($size/$mb, 2)." ".CORE_LAN_MB;
	}
	else if($size < $tb) {
		return round($size/$gb, 2)." ".CORE_LAN_GB;
	} else {
		return round($size/$tb, 2)." ".CORE_LAN_TB;
	}
}

?>